package com.dgshanger.sanhxiaofeisc.ui.person.purse

import android.annotation.SuppressLint
import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.View
import com.dgshanger.sanhxiaofeisc.R
import com.dgshanger.sanhxiaofeisc.common.AppApi
import com.dgshanger.sanhxiaofeisc.common.Constants
import com.dgshanger.sanhxiaofeisc.entity.BillEntity
import com.stone.vega.library.VegaLayoutManager
import com.xx.baseuilibrary.adapter.BaseRecycleAdapter
import com.xx.baseuilibrary.adapter.BaseViewHolder
import com.xx.baseuilibrary.mvp.contract.BaseMvpRecycleViewContract
import com.xx.baseuilibrary.mvp.lcec.BaseMvpRecycleViewFragment
import com.xx.baseutilslibrary.network.entity.BaseResponseEntity
import com.xx.baseutilslibrary.network.rx.RxHelper
import com.xx.baseutilslibrary.network.rx.XxBaseHttpObserver
import io.reactivex.Observable
import kotlinx.android.synthetic.main.item_bill_list.view.*


/**
 * BillListFragment
 * 沉迷学习不能自拔
 * Describe：
 * Created by 雷小星🍀 on 2018/4/18 10:19.
 */
class BillListFragment : BaseMvpRecycleViewFragment<BillEntity>() {
    override val listLimit: Int
        get() = 10
    override val layoutManager: RecyclerView.LayoutManager
        get() = LinearLayoutManager(mContext)

    override fun createAdapter(): BaseRecycleAdapter<BillEntity, out BaseViewHolder<BillEntity>> = Adapter().apply {
        setLoadMoreEnable(true)

        notifyDataSetChanged()
    }

    override fun createModel(): BaseMvpRecycleViewContract.Model<BillEntity> = Model()

    class Model : BaseMvpRecycleViewContract.Model<BillEntity> {

        private var tagSort = ""

        @SuppressLint("NewApi")
        override fun loadData(page: Int, httpObserver: XxBaseHttpObserver<List<BillEntity>>?) {
            if (page == 1) {
                tagSort = ""//刷新时重置Tag
            }
            AppApi.Api()
                    .bill_record(page)
                    .flatMap({ myBillListEntityBaseResponseEntity ->
                        val myBillListEntity = myBillListEntityBaseResponseEntity.data
                        //处理时间排序是否显示
                        for (i in myBillListEntity!!.indices) {
                            val billEntity = myBillListEntity[i]
                            billEntity.isShowTime = (page == 1 && i == 0) || tagSort != billEntity.date_sort
                            tagSort = billEntity.date_sort
                        }
                        Observable.just(BaseResponseEntity(1, myBillListEntity))
                    })
                    .compose(RxHelper.io_main())
                    .subscribe(httpObserver)
        }
    }

    override fun initEvent(view: View?) {
        super.initEvent(view)
        adapter?.setOnItemClickListener { position, entity ->
            val intent = Intent(mContext, BillDetailActivity::class.java)
            intent.putExtra(Constants.KEY_INTENT_LOG_ID, entity.log_id)
            startActivity(intent)
        }
    }

    class Adapter : BaseRecycleAdapter<BillEntity, ViewHolder>() {

        override fun createViewHolder(view: View?, viewType: Int): ViewHolder = ViewHolder(view!!)

        override fun getItemLayout(viewType: Int): Int = R.layout.item_bill_list
    }

    class ViewHolder(view: View) : BaseViewHolder<BillEntity>(view) {

        @SuppressLint("SetTextI18n")
        override fun bindView(position: Int, entity: BillEntity?) {

            itemView.tv_date_sort.visibility = if (entity!!.isShowTime) {
                View.VISIBLE
            } else {
                View.GONE
            }

            with(entity) {
                itemView.tv_created_at.text = created_at
                itemView.tv_shop_name.text = display_title
                itemView.tv_pay_integral.text = "${if ("1".equals(integral_stastus)) "+" else "-"}$integral_money"
                itemView.tv_pay_wallet_money.text = "${if ("1".equals(wallet_status)) "+" else "-"}$wallet_money"
                itemView.tv_date_sort.text = date_sort
            }
        }
    }
}